package com.yangzhi.chimeddetcompose

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import com.yangzhi.chimeddetcompose.ui.theme.ChiMedDetComposeTheme

class MainActivity : ComponentActivity() {
    private val MAX_LENGTH = 12

    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ChiMedDetComposeTheme {
                Scaffold(
                    topBar = {
                        TopAppBar(
                            title = {
                                Text(
                                    text = "中医体质检测系统",
                                    style = TextStyle(fontSize = TextUnit(24f, TextUnitType.Sp))
                                )
                            }, colors = TopAppBarDefaults.topAppBarColors(
                                containerColor = Color(0xFFD2BCFD), // 设置背景颜色
                                titleContentColor = Color.Black // 可选：设置标题颜色
                            )
                        )
                    }, modifier = Modifier.fillMaxSize(),
                ) { innerPadding ->
                    Box(modifier = Modifier.padding(innerPadding)) {
                        Text(
                            text = "欢迎来到中医体质检测系统",
                            modifier = Modifier.padding(start = 20.dp, top = 30.dp),
                            color = Color(0xFF9A29AE),
                            fontWeight = FontWeight.Bold,
                            style = TextStyle(fontSize = TextUnit(24f, TextUnitType.Sp))
                        )
                        Column(
                            horizontalAlignment = Alignment.CenterHorizontally,
                            verticalArrangement = Arrangement.Center,
                            modifier = Modifier
                                .fillMaxWidth()
                                .fillMaxHeight()

                        ) {
                            var account by remember { mutableStateOf("") }
                            var password by remember { mutableStateOf("") }
                            TextField(
                                value = account,
                                textStyle = TextStyle(fontSize = TextUnit(12f, TextUnitType.Sp)),
                                onValueChange = {
                                    account = it
                                },
                                label = {
                                    Text("请输入账号")
                                },
                                maxLines = 1,
                                supportingText = {
                                    Text(
                                        "${account.length}/${MAX_LENGTH}",
                                        modifier = Modifier
                                            .fillMaxWidth()
                                            .padding(end = 0.dp),
                                        textAlign = TextAlign.End
                                    )
                                },
                                leadingIcon = {
                                    Icon(
                                        imageVector = Icons.Filled.AccountBox,
                                        contentDescription = null
                                    )
                                },
                                trailingIcon = {
                                    IconButton(onClick = {
                                        Toast.makeText(baseContext, "账号", Toast.LENGTH_SHORT)
                                            .show();
                                    }) {
                                        Icon(
                                            imageVector = Icons.Filled.Info,
                                            contentDescription = null
                                        )
                                    }
                                },
                                modifier = Modifier.fillMaxWidth(.7f),
                                colors = TextFieldDefaults.colors(
                                    focusedContainerColor = Color.Transparent,
                                    unfocusedContainerColor = Color.Transparent,
                                )
                            )
                            TextField(
                                value = password,
                                onValueChange = {
                                    password = it
                                },
                                label = {
                                    Text("请输入密码")
                                },
                                maxLines = 1,
                                supportingText = {
                                    Text(
                                        "${password.length}/${MAX_LENGTH}",
                                        modifier = Modifier
                                            .fillMaxWidth()
                                            .padding(end = 0.dp),
                                        textAlign = TextAlign.End
                                    )
                                },
                                leadingIcon = {
                                    Icon(
                                        imageVector = Icons.Filled.Lock, contentDescription = null
                                    )
                                },
                                trailingIcon = {
                                    IconButton(onClick = {
                                        Toast.makeText(baseContext, "密码", Toast.LENGTH_SHORT)
                                            .show()
                                    }) {
                                        Icon(
                                            imageVector = Icons.Filled.Info,
                                            contentDescription = null
                                        )
                                    }
                                },
                                modifier = Modifier
                                    .fillMaxWidth(.7f)
                                    .padding(top = 20.dp),
                                colors = TextFieldDefaults.colors(
                                    focusedContainerColor = Color.Transparent,
                                    unfocusedContainerColor = Color.Transparent,
                                )
                            )
                            Button(
                                onClick = {
                                    Toast.makeText(baseContext, "登录成功", Toast.LENGTH_SHORT)
                                        .show()
                                },
                                colors = ButtonDefaults.buttonColors(
                                    containerColor = Color(0xFF9C29B1),
                                    contentColor = Color.White,
                                ),
                                modifier = Modifier.padding(top = 20.dp),
                                contentPadding = PaddingValues(horizontal = 40.dp, vertical = 10.dp)
                            ) {
                                Text(
                                    text = "登录", style = TextStyle(
                                        fontSize = TextUnit(20f, TextUnitType.Sp),
                                        fontWeight = FontWeight.Bold
                                    )
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}